<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>基于Promises的JavaScript和HTML API</title>
    <script src="js/traceur.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="module">

        //提供了电池当前的充电水平和充电状态
        navigator.getBattery().then(function(value){
            console.log("Batter Level: " + (value.level * 100));
        }, function(reason){
            console.log("Error: " + reason);
        });

        //Web Cryptography API:进行哈希
        function convertStringToArrayBufferView(str) {
            var bytes = new Uint8Array(str.length);
            for (var iii = 0; iii < str.length; iii++) {
                bytes[iii] = str.charCodeAt(iii);
            }
            return bytes;
        }
        function convertArrayBufferToHexaDecimal(buffer) {
            var data_view = new DataView(buffer)
            var iii, len, hex = '', c;
            for(iii = 0, len = data_view.byteLength; iii < len; iii++) {
                c = data_view.getUint8(iii).toString(16);
                if(c.length < 2) {
                    c = '0' + c;
                }
                hex += c;
            }
            return hex;
        }
        window.crypto.subtle.digest({name: "SHA-256"}, convertStringToArrayBufferView("ECMAScript 6")).then(function(result){
            var hash_value = convertArrayBufferToHexaDecimal(result);
            console.log(hash_value);
        });
    </script>
</head>
<body>

</body>
</html>